package com.lifeng.test;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lifeng on 2019/5/23.
 * BeanPostProcessor则是bean初始化前后对bean的一些操作，意思就是说bean在调用构造之后，初始化方法前后进行一些操作
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Autowired
    private ApplicationContext applicationContext;

    public Map<String,AbstractPayType> payTypeMap = new HashMap<String,AbstractPayType>();

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetCls = bean.getClass();
        MyAnnotation myAnnotation = targetCls.getAnnotation(MyAnnotation.class);
        if(myAnnotation != null){
            payTypeMap.put(myAnnotation.value(),(AbstractPayType)bean);
        }
        return bean;
    }

    public AbstractPayType getPayType(String payType){
        return payTypeMap.get(payType);
    }
}
